<template>
  <div class="register-bg">
    <el-card class="register-card">
      <h2 class="register-title">用户注册</h2>
      <el-form :model="form" @submit.prevent="handleRegister" class="register-form">
        <el-form-item>
          <el-input v-model="form.username" placeholder="用户名" prefix-icon="el-icon-user" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.password" type="password" placeholder="密码" prefix-icon="el-icon-lock" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.email" placeholder="邮箱" prefix-icon="el-icon-message" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="register-btn" @click="handleRegister" style="width:100%">注册</el-button>
        </el-form-item>
      </el-form>
      <div class="register-link">
        已有账号？<router-link to="/login">去登录</router-link>
      </div>
      <p v-if="error" class="register-error">{{ error }}</p>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const form = ref({ username: '', password: '', email: '' })
const error = ref('')

const handleRegister = async () => {
  error.value = ''
  try {
    await axios.post('/api/auth/register', form.value)
    router.push('/login')
  } catch (e) {
    error.value = e.response?.data?.message || '注册失败'
  }
}
</script>

<style scoped>
.register-bg {
  min-height: 100vh;
  background: #f5f6fa;
  display: flex;
  align-items: center;
  justify-content: center;
}
.register-card {
  width: 400px;
  padding: 40px 30px 30px 30px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  border-radius: 12px;
  background: #fff;
}
.register-title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 30px;
  color: #222;
}
.register-form {
  margin-bottom: 10px;
}
.register-link {
  text-align: center;
  margin-top: 10px;
  color: #888;
}
.register-link a {
  color: #409EFF;
  text-decoration: none;
}
.register-error {
  color: #f56c6c;
  text-align: center;
  margin-top: 10px;
}
</style> 